package de.fhtrier.gdw2.ships.states;

import de.fhtrier.gdw2.ships.SpaceshipGame;
import de.fhtrier.gdw2.ships.menu.MenuManager;
import de.fhtrier.gdw2.ships.utils.AssetLoader;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Menu state
 * 
 * @author Lusito
 * @author Kevin Korte
 */
public class MainMenuState extends BasicGameState {

	int stateID = -1;
	
	MenuManager menuManager;
    private Music music;

	public MainMenuState() {
		this.stateID = SpaceshipGame.MAINMENUSTATE;
	}

	@Override
	public void init(final GameContainer container, final StateBasedGame game)
			throws SlickException {
		menuManager = new MenuManager(container, game, MenuManager.Type.MAINMENU);
        music = AssetLoader.getInstance().getMusic();
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		menuManager.render(container, game, g);
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
    	Input input = container.getInput();
    	if(input.isKeyDown(Input.KEY_ENTER) && (input.isKeyDown(Input.KEY_LALT) ||  input.isKeyDown(Input.KEY_RALT))) {
    		if(container.isFullscreen())
	    		((AppGameContainer)container).setDisplayMode(1024, 768, false);
    		else
	    		((AppGameContainer)container).setDisplayMode(container.getScreenWidth(), container.getScreenHeight(), true);
    	}
    	
		menuManager.update(container, game, delta);
	}

	@Override
	public int getID() {
		return stateID;
	}

	@Override
	public void enter(GameContainer container, StateBasedGame game) throws SlickException {
		menuManager.activate();
        
        if (music != null)
            music.loop(1f, 1f);
	}

	@Override
	public void leave(GameContainer container, StateBasedGame game) throws SlickException {
		menuManager.activate();
	}

	public void keyReleased(int key, char c) {
		menuManager.keyReleased(key, c);
	}

	public void keyPressed(int key, char c) {
		menuManager.keyPressed(key, c);
	}

	public void mouseMoved(int oldx, int oldy, int newx, int newy) {
		menuManager.mouseMoved(oldx, oldy, newx, newy);
	}

	public void mouseDragged(int oldx, int oldy, int newx, int newy) {
		menuManager.mouseDragged(oldx, oldy, newx, newy);
	}

	public void mouseReleased(int button, int x, int y) {
		menuManager.mouseReleased(button, x, y);
	}

	public void mousePressed(int button, int x, int y) {
		menuManager.mousePressed(button, x, y);
	}
	
	public void mouseWheelMoved(int newValue) {
		menuManager.mouseWheelMoved(newValue);
	}

}